package admin.models;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import entities.OwnerDomain;

public class OwnerDomainModel {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "owner_";
	
	private static OwnerDomainModel instance = null;
	
	public static OwnerDomainModel getInstance() {
		if (instance == null) {
			instance = new OwnerDomainModel();
		}
		
		return instance;
	}
	
	private OwnerDomainModel() {
		try {
			ObjectifyService.register(OwnerDomain.class);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }
		
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}
	
	public ArrayList<OwnerDomain> getVerifyDomainChange(int page, int limit) {
		ArrayList<OwnerDomain> domains = new ArrayList<OwnerDomain>();
		
		int start = (page - 1) * limit;
		Query<OwnerDomain> q = ofy.query(OwnerDomain.class).filter("isUpdated =", 0).limit(limit).offset(start);
		for (OwnerDomain domain: q) {
			domains.add(domain);
		}
		
		return domains;
	}
	
	public ArrayList<OwnerDomain> getVertifyOwnerDomain(String username) {
		ArrayList<OwnerDomain> domains = new ArrayList<OwnerDomain>();
		
		List<Key<OwnerDomain>> allKeys = ofy.query(OwnerDomain.class).filter("username", username).filter("status", 1).listKeys();
		
		OwnerDomain ownerDomain = null;
		for (Key<OwnerDomain> key: allKeys) {
			ownerDomain = getOwnerDomain(key.getName());
			if (ownerDomain != null) {
				domains.add(ownerDomain);
			}
		}
		
		return domains;
	}
	
	public ArrayList<OwnerDomain> getUnvertifyOwnerDomain(String username) {
		ArrayList<OwnerDomain> domains = new ArrayList<OwnerDomain>();
		
		List<Key<OwnerDomain>> allKeys = ofy.query(OwnerDomain.class).filter("username", username).filter("status", 0).listKeys();
		
		OwnerDomain ownerDomain = null;
		for (Key<OwnerDomain> key: allKeys) {
			ownerDomain = getOwnerDomain(key.getName());
			if (ownerDomain != null) {
				domains.add(ownerDomain);
			}
		}
		
		return domains;
	}
	
	public OwnerDomain getOwnerDomain(String id) {
		OwnerDomain ownerDomain = new OwnerDomain();
		
		if (cache != null && cache.containsKey(prefix + id)) {
			ownerDomain = (OwnerDomain) cache.get(prefix + id);
		} else {	
			try {
				ownerDomain = ofy.get(new Key<OwnerDomain>(OwnerDomain.class, id));
				cache.put(prefix + id, ownerDomain);
			} catch (Exception e) {
				ownerDomain = null;
			}
		}
		return ownerDomain;
	}
	
	public OwnerDomain getByDomain(String domainName) {
		OwnerDomain ownerDomain = new OwnerDomain();
		if (cache != null && cache.containsKey(prefix + domainName)) {
			ownerDomain = (OwnerDomain) cache.get(prefix + domainName);
		} else {
			try {
				ownerDomain = ofy.query(OwnerDomain.class).filter("domain", domainName).get();
				cache.put(prefix + domainName, ownerDomain);
			} catch (Exception e) {
				ownerDomain = null;
			}
		}
		return ownerDomain;
	}
	
	public boolean checkExists(String id) {
		boolean exists = false;
		OwnerDomain ownerModel = getOwnerDomain(id);
		if (ownerModel != null) {
			exists = true;
		}
		
		return exists;
	}
	
	public void insert(OwnerDomain ownerDomain) {
		if (cache != null) {
			cache.put(prefix + ownerDomain.getId(), ownerDomain);
			cache.put(prefix + ownerDomain.getDomain(), ownerDomain);
		}
		
		ofy.put(ownerDomain);
	}
	
	public void delete(OwnerDomain ownerDomain) {
		if (cache != null && cache.containsKey(prefix + ownerDomain.getId())) {
			cache.remove(prefix + ownerDomain.getId());
		}
		
		if (cache != null && cache.containsKey(prefix + ownerDomain.getDomain())) {
			cache.remove(prefix + ownerDomain.getDomain());
		}
		ofy.delete(ownerDomain);
	}
}
